package org.xyl.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.xyl.model.DiagnosisRequest;
import org.xyl.model.DiagnosisResult;
import org.xyl.service.PriceTagDiagnosisService;

/**
 * 价签系统诊断控制器
 */
@RestController
@RequestMapping("/api/diagnosis")
public class PriceTagDiagnosisController {

    private static final Logger logger = LoggerFactory.getLogger(PriceTagDiagnosisController.class);
    
    private final PriceTagDiagnosisService diagnosisService;
    
    @Autowired
    public PriceTagDiagnosisController(PriceTagDiagnosisService diagnosisService) {
        this.diagnosisService = diagnosisService;
    }
    
    /**
     * 执行价签系统诊断
     */
    @PostMapping("/run")
    public ResponseEntity<DiagnosisResult> runDiagnosis(@RequestBody DiagnosisRequest request) {
        logger.info("收到诊断请求");
        
        try {
            // 验证请求参数
            validateRequest(request);
            
            // 执行诊断
            DiagnosisResult result = diagnosisService.diagnose(request);
            
            logger.info("诊断完成");
            return ResponseEntity.ok(result);
        } catch (IllegalArgumentException e) {
            logger.warn("诊断请求参数错误: {}", e.getMessage());
            return ResponseEntity.badRequest().build();
        } catch (Exception e) {
            logger.error("诊断过程中发生错误", e);
            return ResponseEntity.internalServerError().build();
        }
    }
    
    /**
     * 获取问题解决方案
     */
    @GetMapping("/solution/{problemCode}")
    public ResponseEntity<String> getSolution(@PathVariable String problemCode) {
        logger.info("查询解决方案: 问题代码={}", problemCode);
        
        try {
            String solution = diagnosisService.getSolution(problemCode);
            return ResponseEntity.ok(solution);
        } catch (Exception e) {
            logger.error("获取解决方案失败", e);
            return ResponseEntity.internalServerError().build();
        }
    }
    
    /**
     * 获取沃尔玛超市价签问题的快速诊断
     */
    @GetMapping("/walmart/quick-check")
    public ResponseEntity<String> walmartQuickCheck() {
        logger.info("执行沃尔玛超市快速诊断");
        
        try {
            // 简化实现，直接返回沃尔玛超市价签问题的常见解决方案
            String summary = "深圳市宝安区沃尔玛超市价签刷图失败诊断结果:\n" +
                            "\n问题分析:\n" +
                            "1. 主要原因: 网络超时和服务器负载过高\n" +
                            "2. 具体分析: 沃尔玛超市规模大，价签数量多，网络覆盖复杂\n" +
                            "3. 可能因素: RF信号干扰、WiFi覆盖不良、并发请求过多\n" +
                            "\n推荐解决方案:\n" +
                            "1. 优化网络拓扑，增加接入点\n" +
                            "2. 实施分批更新策略，避开高峰期\n" +
                            "3. 检查并调整RF信号频率以减少干扰\n" +
                            "4. 增加服务器资源或优化数据库查询性能";
            
            return ResponseEntity.ok(summary);
        } catch (Exception e) {
            logger.error("快速诊断失败", e);
            return ResponseEntity.internalServerError().body("快速诊断失败: " + e.getMessage());
        }
    }
    
    private void validateRequest(DiagnosisRequest request) {
        // 简化验证
        if (request == null) {
            throw new IllegalArgumentException("请求参数不能为空");
        }
    }
}
